package top.lingkang.fas.common.dto;

import lombok.Data;
import org.noear.snack.ONode;
import top.lingkang.fas.common.http.HttpStatus;

/**
 * @author lingkang
 * created by 2025/7/20 1:23
 */
@Data
public class R<T> {
    private int code;
    private String msg = "操作成功";
    private T data;

    public R(int code) {
        this.code = code;
    }

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(T data, int code) {
        this.data = data;
        this.code = code;
    }

    public R(T data) {
        this.data = data;
    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public R(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public static <T> R<T> ok() {
        return new R<>(HttpStatus.SUCCESS);
    }

    public static <T> R<T> ok(String msg) {
        return new R<>(HttpStatus.SUCCESS, msg);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(HttpStatus.SUCCESS, data);
    }

    public static <T> R<T> ok(String msg, T data) {
        return new R<>(HttpStatus.SUCCESS, msg, data);
    }

    public static <T> R<T> ok(int code, T data) {
        return new R<>(code, data);
    }

    public static <T> R<T> fail(String msg) {
        return new R<>(HttpStatus.ERROR, msg);
    }

    public static <T> R<T> fail(int code, String msg) {
        return new R<>(code, msg);
    }


    @Override
    public String toString() {
        return toJson();
    }

    public String toJson() {
        return ONode.load(this).toJson();
    }
}
